# -*- coding: utf-8 -*-
"""
Created on Thu Jan 17 12:25:07 2019

@author: william

Email: hua_yan_tsn@163.com
"""

import tensorflow as tf
sess = tf.Session()
def lossFunction(prediction_vector, truth_vector):
    """
    :param prediction_vector: 预测结果的向量, 类型是TensorFlow的tensor
    :param truth_vector: 真实结果的向量， 类型是TensorFlow的tensor类型
    :param truth_vector: 真实结果的向量， 类型是TensorFlow的tensor类型
    :return: 损失函数的计算值
    """
    ln_prediction = tf.log(prediction_vector)
    (m, c) = prediction_vector.shape
    n = tf.multiply(m, c)
    answer = tf.multiply(truth_vector, ln_prediction)
    sess.run(ln_prediction)
    sess.run(answer)
    sess.run(n)
    result = -1/n * answer
    result = tf.reduce_sum(result)
    print('Loss value is %6.3f' % result.eval(session=sess))
    return result

def training():
    pass

def main():
    A = tf.Variable(tf.random_normal([5, 5], 5, 1, dtype=tf.float64))
    B = tf.Variable(tf.random_normal([5, 5], 5, 1, dtype=tf.float64))
    init = tf.global_variables_initializer()
    sess.run(init)
    lossFunction(A, B)
    sess.close()

if __name__ == '__main__':
    main()